package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadLetterMqConfig {

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    @Bean   //交换机应答
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }

    @Bean   //死信队列      普通队列中设置等待时间，转发携带，
    public Queue deadQueue(){
        return QueueBuilder.durable(queue_dead_1).build();
    }

    @Bean   //普通队列
    public Queue queue(){
        Map<String,Object> map = new HashMap<>();
        //1:转发的死信交换机是谁？
        map.put("x-dead-letter-exchange",exchange_dead);
        //2:转发的死信RoutingKey是谁？
        map.put("x-dead-letter-routing-key",routing_dead_1);
        //3:TTL是多少  全局  毫秒
        map.put("x-message-ttl",10 * 1000);
        return QueueBuilder.durable(queue_dead_2).withArguments(map).build();
    }

    @Bean   //死信队列绑定
    public Binding bindingQueue(){
        return BindingBuilder.bind(deadQueue()).to(exchange()).with(routing_dead_1).noargs();
    }

    @Bean   //普通队列绑定
    public Binding bindingDeadQueue(){
        return BindingBuilder.bind(queue()).to(exchange()).with(routing_dead_2).noargs();
    }
}
